package com.html;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.util.Log;

import com.application.VideosList;
import com.entity.YoukuVideo;
import com.videoplayer.R;

public class VideoListResolve {

	private Context context;
	public YoukuVideo youkuList;
	private VideosList videoList;
	public Document doc;
	private String currentContent;
	private String currentTitle;

	public String getCurrentTitle() {
		return currentTitle;
	}

	public void setCurrentTitle(String currentTitle) {
		this.currentTitle = currentTitle;
	}

	public VideoListResolve(Context c) {
		context = c;
		videoList = (VideosList) c.getApplicationContext();
	}

	public String getCurrentContent() {
		return currentContent;
	}
	

	public String[] getVideoTitle() {
		if ("腾讯".endsWith(currentContent)) {
			return context.getResources().getStringArray(R.array.tecent_one);
		} else if ("优酷".equals(currentContent)) {
			return context.getResources().getStringArray(R.array.name_one);
		} else {
			return null;
		}

	}

	public String[] getVideoTitleUrl() {
		if ("腾讯".endsWith(currentContent)) {
			return context.getResources()
					.getStringArray(R.array.tecent_one_url);
		} else if ("优酷".equals(currentContent)) {
			return context.getResources().getStringArray(R.array.name_one_url);
		} else {
			return null;
		}
	}

	public String getVideoList(String states) {
		// List<YoukuVideo> videoList = new ArrayList<YoukuVideo>();
		if (states.contains("qq.com")) {
			currentContent = "腾讯";
			if ("http://v.qq.com/".equals(states)) {
				currentTitle = "腾讯首页";
			}
			try {

				doc = Jsoup.connect(states).timeout(60000).get();
				// youkuList = new YoukuVideo();
				Elements content = doc.getElementsByClass("list_item");
				// System.out.println("222222222222222222222"+);
				for (Element link : content) {
					Elements linkHref = link.getElementsByClass("figure_title");
					Elements linkImg = link.getElementsByClass("figure");

					String href = "";
					String vThumb = "";
					String lImg = "";
					String vClarity = "";
					String vTime = "";
					for (Element link3 : linkImg) {
						if ("".equals(lImg = link3.getElementsByTag("img")
								.attr("src"))) {
							lImg = link3.getElementsByTag("img").attr("lz_src");
						} else {
							lImg = link3.getElementsByTag("img").attr("src");
						}
					}
					for (Element link2 : linkHref) {

						href = link2.getElementsByTag("a").attr("href");
						vThumb = link2.getElementsByTag("a").attr("title");
						// Log.e("tengxunList", "result        " + lImg + "^^^"
						// + href + "^^^" + vThumb);
						if (href.contains("/cover/")) {
							if (href.contains("v.qq.com")) {
								youkuList = new YoukuVideo(href, vThumb, lImg,
										vTime, vClarity);
							} else {
								youkuList = new YoukuVideo("http://v.qq.com"
										+ href, vThumb, lImg, vTime, vClarity);
							}
							if (!videoList.getVideoList().contains(youkuList)) {
								videoList.getVideoList().add(youkuList);
								// Log.v("youkuList", href + "------" + vThumb
								// + "--------" + lImg);

							}
						}

						// System.out.println(vThumb);
						// System.out.println(href);

					}

					// String linkText = link.text();

				}
				String[] className = context.getResources().getStringArray(
						R.array.tecent_class);
				for (int i = 0; i < className.length; i++) {
					Elements content2 = doc.getElementsByClass(className[i]);

					for (Element link : content2) {
						Elements content3 = link.getElementsByTag("li");
						for (Element link2 : content3) {
							String href = link2.getElementsByTag("a").attr(
									"href");
							String vThumb = link2.getElementsByTag("img").attr(
									"alt");
							String lImg = "";
							if ("".equals(lImg = link2.getElementsByTag("img")
									.attr("src"))) {
								lImg = link2.getElementsByTag("img").attr(
										"lz_src");
							} else {
								lImg = link2.getElementsByTag("img")
										.attr("src");
							}
							if ("".equals(lImg)) {
								lImg = link2.getElementsByTag("img").attr(
										"_src");
							}
							if ("".equals(vThumb)) {
								vThumb = link2.getElementsByTag("a").text();
							}
							if (href.contains("/cover/")
									|| href.contains("v.qq.com")) {
								if(href.contains("film.qq.com"))
									continue;
								if (href.contains("v.qq.com")) {
									youkuList = new YoukuVideo(href, vThumb,
											lImg, "", "");
								} else {
									youkuList = new YoukuVideo(
											"http://v.qq.com" + href, vThumb,
											lImg, "", "");
								}
								if (!videoList.getVideoList().contains(
										youkuList)) {
									videoList.getVideoList().add(youkuList);
									// Log.v("youkuList", "&&&&&&&"+
									// lImg+"--"+vThumb);

								}
							}
						}

					}

					int j = 0;
					List<String> temp = new ArrayList<String>();
					for (Element link : content2) {
						Elements content3 = link.getElementsByTag("div");
						for (Element link3 : content3) {
							// Elements content4 =
							// link2.getElementsByTag("div");
							// for (Element link3 : content4) {
							if (j != 0) {
//								Log.v("youkuList", "$$$$$$$" + link3);
//								Log.v("youkuList",
//										"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");

								String href = link3.getElementsByTag("a").attr(
										"href");

								String vThumb = link3.getElementsByTag("img")
										.attr("alt");

								String lImg = "";
								if ("".equals(lImg = link3.getElementsByTag(
										"img").attr("src"))) {
									lImg = link3.getElementsByTag("img").attr(
											"lz_src");
								} else {
									lImg = link3.getElementsByTag("img").attr(
											"src");
								}
								if ("".equals(lImg)) {
									lImg = link3.getElementsByTag("img").attr(
											"_src");
								}
								if ("".equals(vThumb)) {
									vThumb = link3.getElementsByTag("a").text();
								}
								if (temp.contains(href.trim())
										|| temp.contains(lImg.trim())) {
									continue;
								}
								temp.add(href.trim());
								temp.add(lImg.trim());
								if (href.contains("/cover/")
										|| href.contains("v.qq.com")) {
									if (href.contains("v.qq.com")) {
										youkuList = new YoukuVideo(href,
												vThumb, lImg, "", "");
									} else {
										youkuList = new YoukuVideo(
												"http://v.qq.com" + href,
												vThumb, lImg, "", "");
									}
										videoList.getVideoList().add(youkuList);
//										Log.e("youkuList", href + "------"
//												+ vThumb + "--------" + lImg);

								}
								// }

							}
							j++;
						}

					}
				}

				return "success";

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "failed";
			}
		} else if (states.contains("youku.com")) {
			currentContent = "优酷";
			if ("http://www.youku.com".equals(states)) {
				currentTitle = "优酷首页";
			}
			// List<YoukuVideo> videoList = new ArrayList<YoukuVideo>();
			try {

				doc = Jsoup.connect(states).timeout(60000).get();
				// youkuList = new YoukuVideo();

				Elements content = doc.getElementsByClass("v");
				// System.out.println("222222222222222222222"+);
				for (Element link : content) {
					Elements linkHref = link.getElementsByClass("v_title");
					Elements linkImg = link.getElementsByClass("v_thumb");
					Elements linkClarity = link.getElementsByClass("v_ishd");
					Elements linkTime = link.getElementsByClass("v_time");

					String href = "";
					String vThumb = "";
					String lImg = "";
					String vClarity = "";
					String vTime = "";
					for (Element link3 : linkImg) {
						if ("".equals(link3.getElementsByTag("img")
								.attr("_src"))) {
							lImg = link3.getElementsByTag("img").attr("src");
						} else {
							lImg = link3.getElementsByTag("img").attr("_src");
						}
					}
					for (Element linkclarity : linkClarity) {
						vClarity = linkclarity.children().attr("title");
					}
					for (Element linktime : linkTime) {
						vTime = linktime.children().text();
					}
					for (Element link2 : linkHref) {

						href = link2.children().attr("href");
						vThumb = link2.children().attr("title");
						if (href.contains("v.youku.com/v_show/id")) {
							youkuList = new YoukuVideo(href, vThumb, lImg,
									vTime, vClarity);
							if (!videoList.getVideoList().contains(youkuList)) {
								videoList.getVideoList().add(youkuList);
//								Log.v("youkuList", href + "------" + vThumb
//										+ "--------" + lImg);

							}
						}

						// System.out.println(vThumb);
						// System.out.println(href);

					}

					// String linkText = link.text();

				}

				Elements content0 = doc.getElementsByClass("p");
				// System.out.println("222222222222222222222"+);
				if (content0 != null) {
					for (Element link : content0) {
						Elements linkHref = link.getElementsByClass("p_title");
						Elements linkImg = link.getElementsByClass("p_thumb");
						Elements linkClarity = link
								.getElementsByClass("p_ishd");
						Elements linkTime = link.getElementsByClass("p_time");

						String href = "";
						String vThumb = "";
						String lImg = "";
						String vClarity = "";
						String vTime = "";
						for (Element link3 : linkImg) {
							if ("".equals(link3.getElementsByTag("img").attr(
									"_src"))) {
								lImg = link3.getElementsByTag("img")
										.attr("src");
							} else {
								lImg = link3.getElementsByTag("img").attr(
										"_src");
							}
						}
						for (Element linkclarity : linkClarity) {
							vClarity = linkclarity.children().attr("title");
						}
						for (Element linktime : linkTime) {
							vTime = linktime.children().text();
						}
						for (Element link2 : linkHref) {

							href = link2.children().attr("href");
							vThumb = link2.children().attr("title");
							if (href.contains("v.youku.com/v_show/id")) {
								youkuList = new YoukuVideo(href, vThumb, lImg,
										vTime, vClarity);
								if (!videoList.getVideoList().contains(
										youkuList)) {
									videoList.getVideoList().add(youkuList);
//									Log.v("youkuList", href + "------" + vThumb
//											+ "--------" + lImg);

								}
							}

							// System.out.println(vThumb);
							// System.out.println(href);

						}

						// String linkText = link.text();

					}
				}

				/**
				 * youkuList = new YoukuVideo(youku.getFocusVideoURL(doc),
				 * "澶存潯鏂伴椈", ""); videoList.add(0,youkuList);
				 */
				return "success";

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "failed";
			}
		} else {

			return "failed";
		}
	}
}
